//
//  XZRollView.m
//  Teecloud
//
//  Created by PC on 2018/11/20.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZRollView.h"
#import "XZListData.h"
@interface XZRollView ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *alertView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation XZRollView

- (void)awakeFromNib{
    [super awakeFromNib];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (void)setListData:(NSArray *)listData{
    _listData = listData;
    [self.dataSource removeAllObjects];
    [self.dataSource addObjectsFromArray:listData];
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.contentView.backgroundColor = kEFEFEFColor;
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = k333333Color;
    XZListData *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.Name;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    XZListData *model = self.dataSource[indexPath.row];
    if (self.RollViewBlock) {
        self.RollViewBlock(model);
        //隐藏
        [self hide];
    }
}
- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
        [rootWindow addSubview:self];
        [self animationAlert:self.alertView];
    }];
}

- (void)hide{
    [UIView animateWithDuration:0.25 animations:^{
        [self removeFromSuperview];
    }];
    
}
- (void)hideView{
    [UIView animateWithDuration:0.25 animations:^{
        [self removeFromSuperview];
    }];
}
- (void)animationAlert:(UIView *)view
{
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.4;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [view.layer addAnimation:popAnimation forKey:nil];
    
}

- (IBAction)hideTapAction:(UITapGestureRecognizer *)sender {
    //隐藏
    [self hide];
}



@end
